package uk.ac.aber.Blockmation.ColorsTable;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;
import java.awt.Color;
import java.awt.Component;

/**
 * Renders each cell of a table with background color to be equivalent to
 * the color represented by the cell's Color object. It adds a little bit of border
 * so each cell looks like a little square. ToolTip text of each cell is then set
 * to express RGB values of the selected color.
 * @author Aron
 *
 */
public class TableColorRenderer extends JLabel
                           implements TableCellRenderer {
    Border border = null;

    public TableColorRenderer() {
        setOpaque(true); //MUST do this for background to show up.
    }

    public Component getTableCellRendererComponent(
                            JTable table, Object color,
                            boolean isSelected, boolean hasFocus,
                            int row, int column) {
        
        Color newColor = (Color)color;
        setBackground(newColor);
        
        border = BorderFactory.createMatteBorder(2,5,2,5,table.getBackground());
        
        if (hasFocus)
        {
            border = BorderFactory.createMatteBorder(2,5,2,5,
            table.getSelectionBackground());
        }

        
        setBorder(border);

        setToolTipText("RGB value: " + newColor.getRed() + ", "
                                     + newColor.getGreen() + ", "
                                     + newColor.getBlue());
        return this;
    }
}
